home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtool17.zip / ACTLIB.ZIP / TOOLS.ZIP / ERRORLVL.C < prev    next >
C/C++ Source or Header  |  1993-09-20  |  3KB  |  127 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "tools.h"
  4. #include <dos.h>
  5. #include <strings.h>
  6. #include <direct.h>
  7. #include <stdlib.h>
  8. #include <ctype.h>
  9.  
  10.        
  11. /***
  12.  *
  13.  *  Function    :  geterrorlevel
  14.  *
  15.  *  Description :  Return errorlevel code from previous command executed
  16.  *                 by current shell (COMMAND.COM or equivalent)
  17.  *
  18.  *  Parameters  :  none
  19.  *
  20.  *  Return      :  errorlevel
  21.  *                 -1 If MS-DOS version not supported
  22.  *
  23.  *  Warning     :  This function is only valid with shells
  24.  *                 MS-DOS COMMAND.COM 3.20, 3.21, 3.30, 4.0, 4.01, 5.0, 6.0,
  25.  *                 NDOS 6.0,
  26.  *                 4DOS 4.02
  27.  */
  28.  
  29. int geterrorlevel( void )
  30. {
  31.  unsigned psp, far *tmp;
  32.  int offset, version;
  33.  char *shell, *prgname;
  34.  
  35.         /* _psp is our PSP.  Follow the "parent" links back
  36.            until we find a PSP for COMMAND.COM, which has
  37.            a parent link that points at itself. */
  38.  
  39.  for ( psp = _psp; psp != *(tmp = MK_FP(psp,0x16)); psp = *tmp );
  40.  
  41.  
  42.         /* locate environment */
  43.  prgname = MK_FP( *(unsigned far *)MK_FP(psp,0x2C), 0 );
  44.  while ( *prgname ) while ( *prgname++ );  /* locate end of environment */
  45.  prgname += 3;
  46.  shell = strrchr( prgname, '\\' );
  47.  if ( ! shell )
  48.     shell = strrchr( prgname, ':' );
  49.  if ( ! shell )
  50.     shell = prgname;
  51.  else
  52.     shell++;
  53.  
  54.  /* offset depends of shell version */
  55.  version = _osmajor * 100 + _osminor;
  56.  
  57.  switch( toupper(*shell) )
  58.  {
  59.   case 'N': /***   NDOS   ***/
  60.      {
  61.       union REGS regs;
  62.       regs.x.ax = 0xE44D;
  63.       regs.h.bh = 0;
  64.       int86( 0x2F, ®s, ®s );
  65.       if ( regs.x.ax != 0x44EE )
  66.          return -1;
  67.  
  68.       switch( regs.h.bl * 100 + regs.h.bh ) /* version */
  69.       {
  70.        case 100: offset = 0x3FAB; break; /* NU 6.0 */
  71.        default : return -1;
  72.       }
  73.      }
  74.      break;
  75.  
  76.   case '4': /***   4DOS   ***/
  77.      {
  78.       union REGS regs;
  79.       regs.x.ax = 0xD44D;
  80.       regs.h.bh = 0;
  81.       int86( 0x2F, ®s, ®s );
  82.       if ( regs.x.ax != 0x44DD )
  83.          return -1;
  84.  
  85.       switch( regs.h.bl * 100 + regs.h.bh ) /* version */
  86.       {
  87.        case 420: offset = 0x01E6; break;
  88.        default : return -1;
  89.       }
  90.      }
  91.      break;
  92.  
  93.   default : /***   COMMAND.COM: MS-DOS or DR-DOS   ***/
  94.      {
  95.       /* Check for DR-DOS */
  96.       union REGS regs;
  97.       regs.x.ax = 0x4452;
  98.       regs.x.cflag = 1;
  99.       int86( 0x21, ®s, ®s );
  100.       if ( ! regs.x.cflag )
  101.          {
  102.           switch( atoi(getenv("VER")) )
  103.           {
  104.            case 500: offset = 0x44C1; break; /* or 0AE8 ? */
  105.            case 600: offset = 0x474D; break;
  106.            default : return -1;
  107.           }
  108.          }
  109.        else
  110.           switch( version )
  111.           {
  112.            case 320:
  113.            case 321: offset = 0x0AFA /* or 3D31 */; break;
  114.            case 330: offset = 0x0BEA; break;
  115.            case 400: offset = 0x0F2B; break;
  116.            case 401: offset = 0x0F2C; break;
  117.            case 500: offset = 0x02A3; break;
  118.            case 600: offset = 0x02A5; break;
  119.            default : return -1;
  120.           }
  121.      }
  122.      break;
  123.  }
  124.  
  125.  return *(unsigned far *)MK_FP( psp, offset );
  126. }
  127.